#include "TestTcpClient.h"

TestTcpClient::TestTcpClient(QObject *parent)
    : DataBus::TcpClient(parent),
      m_propertyInfoList(),
      m_testPropertyValue(0)
{
    m_propertyInfoList.append(DataBus::PropertyInfo(0,
                                                    DataBus::DataType_Int32,
                                                    DataBus::AccessPermission_ReadOnly,
                                                    QLatin1String("Int32_RO")));
    m_propertyInfoList.append(DataBus::PropertyInfo(1,
                                                    DataBus::DataType_Int32,
                                                    DataBus::AccessPermission_WriteOnly,
                                                    QLatin1String("Int32_WO")));
    m_propertyInfoList.append(DataBus::PropertyInfo(2,
                                                    DataBus::DataType_Int32,
                                                    DataBus::AccessPermission_ReadWrite,
                                                    QLatin1String("Int32_RW")));
}

TestTcpClient::~TestTcpClient()
{
}

void TestTcpClient::processReceivedPacket(const DataBus::Packet &packet)
{
    emit packetReceived(packet);
}

QList<quint8> TestTcpClient::getPropertyIdList() const
{
    QList<quint8> list;

    for (int i = 0; i < m_propertyInfoList.size(); i++)
    {
        list.append(m_propertyInfoList.at(i).getId());
    }

    return list;
}

bool TestTcpClient::getPropertyInfo(const quint8 propertyId, DataBus::PropertyInfo *propertyInfo) const
{
    if (propertyInfo == 0)
    {
        return false;
    }

    for (int i = 0; i < m_propertyInfoList.size(); i++)
    {
        if (propertyId == m_propertyInfoList.at(i).getId())
        {
            *propertyInfo = m_propertyInfoList.at(i);
            return true;
        }
    }

    return false;
}

bool TestTcpClient::getPropertyValue(const quint8 propertyId, DataBus::Value *propertyValue)
{
    if (propertyValue == 0)
    {
        return false;
    }

    switch (propertyId)
    {
        case 0:
        case 2:
        {
            propertyValue->setInt32(m_testPropertyValue);
            return true;
        }

        default:
        {
            return false;
        }
    }
}

bool TestTcpClient::setPropertyValue(const quint8 propertyId, const DataBus::Value &propertyValue)
{
    switch (propertyId)
    {
        case 1:
        case 2:
        {
            if (propertyValue.getDataType() != DataBus::DataType_Int32)
            {
                return false;
            }

            m_testPropertyValue = propertyValue.getInt32();
            return true;
        }

        default:
        {
            return false;
        }
    }
}
